package com.tk.thread;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public class TestThread extends Thread {
    private String url;
    private String name;

    public TestThread(String url, String name) {
        this.url = url;
        this.name = name;
    }

    @Override
    public void run() {
        WebDownLoader webDownLoader = new WebDownLoader();
        webDownLoader.downloader(url, name);
        System.out.println("下载了文件名为：" + name);
    }

    public static void main(String[] args) {
        TestThread t1 = new TestThread("https://img-pre.ivsky.com/img/tupian/pre/202005/30/cat-002.jpg", "1.jpg");
        TestThread t2 = new TestThread("https://img-pre.ivsky.com/img/tupian/pre/202005/30/cat-003.jpg", "2.jpg");
        TestThread t3 = new TestThread("https://img-pre.ivsky.com/img/tupian/pre/202005/30/cat-004.jpg", "3.jpg");

        t1.start();
        t2.start();
        t3.start();
    }
}

class WebDownLoader {
    public void downloader(String url, String name) {
        try {
            FileUtils.copyURLToFile(new URL(url), new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常，downloader方法出现问题");
        }
    }
}
